<?
    include "../../common.php" ;
    $opt = $_POST['opt'] ; 
    $arr = [] ; 
    if( $opt == 'cateSelect' ){
        $str = '' ;
        $sql = "SELECT * FROM pcate WHERE subid is null ORDER BY id ASC" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $dataArr[] = $one ; 
        }
        foreach( $dataArr as $key  => $value ){
            $str .= "<option value='{$value["id"]}' value1='{$value["cateName"]}'>{$value['cateName']}</option>" ;
        }
        foreach( $dataArr as $k => $v ){
            $sql1 = "SELECT * FROM pcate WHERE subid = {$v['id']}" ;
            $res1 = mysqli_query($link,$sql1);
            $dataArr1 = [] ;
            while( $one = mysqli_fetch_assoc($res1) ){
                $dataArr1[] = $one ; 
            }
            foreach( $dataArr1 as $i => $item ){
                $str .= "<option value='{$item["id"]}' value1='{$item["subCateName"]}'>{$item['subCateName']}</option>" ;
            }
        }
        $arr['data'] = $str ; 
    }
    if( $opt == 'getDataCate' ){
        $myid = $_POST['myid'] ; 
        $sql = "SELECT * FROM pcate WHERE id = {$myid}";
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr = $one ; 
        }
    }
    if( $opt == 'getDataSub' ){
        $myid = $_POST['myid'] ; 
        $sql0 = "SELECT * FROM pcate WHERE id = {$myid}";
        $res0 = mysqli_query($link,$sql0);
        if( $one = mysqli_fetch_assoc($res0) ){
            $arr = $one ; 
        }

        $str = '' ;
        $sql = "SELECT * FROM pcate WHERE subid is null ORDER BY id ASC" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $dataArr[] = $one ; 
        }
        foreach( $dataArr as $key  => $value ){
            if( $arr['subid'] == $value['id'] ){
                $str .= "<option value='{$value["id"]}' value1='{$value["cateName"]}' selected>{$value['cateName']}</option>" ;
            } else {
                $str .= "<option value='{$value["id"]}' value1='{$value["cateName"]}'>{$value['cateName']}</option>" ;
            }
            
        }
        foreach( $dataArr as $k => $v ){
            $sql1 = "SELECT * FROM pcate WHERE subid = {$v['id']}" ;
            $res1 = mysqli_query($link,$sql1);
            $dataArr1 = [] ;
            while( $one = mysqli_fetch_assoc($res1) ){
                $dataArr1[] = $one ; 
            }
            foreach( $dataArr1 as $i => $item ){
                if( $arr['subid'] == $item['id'] ){
                    $str .= "<option value='{$item["id"]}' value1='{$item["subCateName"]}' selected>{$item['subCateName']}</option>" ;
                } else {
                    $str .= "<option value='{$item["id"]}' value1='{$item["subCateName"]}'>{$item['subCateName']}</option>" ;
                }
            }
        }
        $arr['options'] = $str ; 
    }
    echo json_encode($arr);
?>